#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <unordered_map>
// 湍流子数组的最大长度：https://leetcode.cn/problems/longest-turbulent-subarray/description/
using namespace std;
class Solution {
public:
    int maxTurbulenceSize(vector<int>& nums) {
        int n=nums.size();
        if(n==1) return 1;
        vector<int> dp(n);
        dp[0]=1;
        char s='*';
        for(int i=1;i<n;i++)
        {
            if(s=='>'&&nums[i-1]<nums[i])
            {
                dp[i]=dp[i-1]+1;
                s='<';
            }
            else if(s=='<'&&nums[i-1]>nums[i])
            {
                dp[i]=dp[i-1]+1;
                s='>';
            }
            else
            {
                dp[i]=(nums[i-1]==nums[i])?1:2;
                s=nums[i-1]>nums[i]?'>':((nums[i-1]==nums[i])?'*':'<');
            }
        }
        int ret=-0x3f3f3f3f;
        for(int i=0;i<n;i++) ret=max(ret,dp[i]);
        return ret;
    }
};